package hn.cch.apache.commons.lang3;

import hn.cch.apache.commons.CloneClass;
import hn.cch.apache.commons.FieldClass;
import hn.cch.apache.commons.ImplementsSerializable;
import org.apache.commons.lang3.SerializationUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;

/**
 * common-lang
 */
public class Lang3Test {

    private static final Logger logger = LoggerFactory.getLogger(Lang3Test.class);


    @Test
    public void testDateUtils() throws ParseException {


        Date date = new Date();
        String pattern = "yyyy-MM-dd HH:mm:ss";
        String format = DateFormatUtils.format(date, pattern);
        System.out.println(format);

        Date parseDate = DateUtils.parseDate(format, pattern);
        System.out.println(parseDate);

    }


    @Test
    public void testStringUtils() {
        StringUtils.isEmpty("");
    }

    @Test
    public void testExceptionUtils() {
        Exception exception = new Exception("message");
        logger.error(ExceptionUtils.getStackTrace(exception));
    }


    @Test
    public void testSerializationUtils() {
        // 序列化和反序列化
        ImplementsSerializable implementsSerializable = new ImplementsSerializable("chench");
        byte[] bytes = SerializationUtils.serialize(implementsSerializable);
        Object obj = SerializationUtils.deserialize(bytes);


        // 克隆=序列化+反序列化
        FieldClass fieldClass = new FieldClass();
        fieldClass.setString("chench");
        fieldClass.setImplementsSerializable(implementsSerializable);
        CloneClass cloneClass = new CloneClass();
        cloneClass.setFieldClass(fieldClass);
        cloneClass.setString("chench");

        CloneClass clone = SerializationUtils.clone(cloneClass);
        clone.getFieldClass().getImplementsSerializable().setString("hang");
        clone.getFieldClass().setString("cch");
        clone.setString("hn");

        System.out.println(cloneClass.getFieldClass());
        System.out.println(clone.getFieldClass());


        // Map克隆
        HashMap<String, FieldClass> map = new HashMap<>();
        String key = "fieldClass";
        map.put(key, fieldClass);
        HashMap<String, FieldClass> mapClone = SerializationUtils.clone(map);
        FieldClass field = mapClone.getOrDefault(key, new FieldClass());
        field.setString("field class");
        field.getImplementsSerializable().setString("field clone");
        System.out.println(field);


    }


}
